// 提问仓库

import {
  getReqAPI,
  getMyReqAPI,
  findUserReqComLikeAPI,
  getMyComReqAPI,
} from "@/api";

const state = {
  reqCards: [],
  myReqCards: [],
  recentCards: [],
  userLikeReqComs: [],
  myReqComments: [],
};

const actions = {
  async getReqCards({ commit }, { way, text }) {
    const res = await getReqAPI({ way, text });
    if (res.status === 0) {
      commit("GETREQCARDS", res.data);
    }
  },
  async getMyReqCard({ commit }) {
    const res = await getMyReqAPI();
    if (res.status === 0) {
      commit("GETMYREQCARD", res.data);
    }
  },
  async getRecentCard({ commit }, { way, text }) {
    let res = await getReqAPI({ way, text });
    console.log("resrwsrw", res);
    if (res.status === 0) {
      commit("GETRECENTCARD", res.data);
    }
  },
  // 获取该用户点赞的评论卡片id
  async getUserLikeReqComID({ commit }) {
    let res = await findUserReqComLikeAPI();
    console.log("用户点赞的评论id", res);
    if (res.status === 0) {
      commit("GETUSERLIKEREQCOMID", res.data);
    }
  },
  async getMyComReq({ commit }) {
    let res = await getMyComReqAPI();
    console.log("用户的个人评论卡片", res);
    if (res.status === 0) {
      commit("GETMYCOMREQ", res.data);
    }
  },
};

const mutations = {
  GETREQCARDS(state, data) {
    state.reqCards = data;
  },
  GETMYREQCARD(state, data) {
    state.myReqCards = data;
  },
  GETRECENTCARD(state, data) {
    state.recentCards = data;
  },
  GETUSERLIKEREQCOMID(state, data) {
    state.userLikeReqComs = data;
  },
  GETMYCOMREQ(state, data) {
    state.myReqComments = data;
  },
};

const getters = {
  userLikeReqComsIds: (state) => {
    return state.userLikeReqComs.map((likeReqCom) => likeReqCom.comment_id);
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
